package com.graduate.cec.controller;

import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.JsonResult;
import com.graduate.cec.pojo.dto.ArticleDTO;
import com.graduate.cec.pojo.dto.ArticleOperateDTO;
import com.graduate.cec.pojo.dto.ArticleSimpleDTO;
import com.graduate.cec.pojo.dto.ArticleSimplePageDTO;
import com.graduate.cec.pojo.entity.Article;
import com.graduate.cec.pojo.entity.Nation;
import com.graduate.cec.pojo.vo.ArticleDetailVO;
import com.graduate.cec.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/article",produces = "application/json;charset=utf-8")
@Api(tags = "文章管理")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    /****************        增       ******************************************************************/
    @PostMapping("/insert-article")
    @ApiOperation("增加文章")
    public JsonResult<Long> insertArticle(@RequestBody ArticleDTO article){

        return JsonResult.ok("发布成功", articleService.insertArticle(article));
    }

    //上传图片
    @PostMapping("/upload-picture")
    @ApiOperation("上传图片")
    public JsonResult<String> upload(MultipartFile picFile) throws Exception {
        String fileName = articleService.uploadPictures(picFile);
        return JsonResult.ok("插入成功",fileName);

    }


    /****************        删       ******************************************************************/
    //删除上传的图片
    @GetMapping("/remove-picture")
    @ApiOperation("删除图片")
    public JsonResult<Void> remove(String pictureName) {
        articleService.removePicture(pictureName);
        return JsonResult.ok("删除成功");
    }


    @GetMapping("/delete-by-keys/{ids}")
    @ApiOperation("通过主键删除文章")
    public JsonResult<Void> deleteByKeys(@PathVariable("ids") List<Long> ids){
        log.info("ids:{}", ids);
        articleService.deleteByKeys(ids);
        return JsonResult.ok("删除成功");
    }


    /****************        改       ******************************************************************/
    @PostMapping("/update-by-key")
    @ApiOperation("通过主键修改文章内容")
    public JsonResult<Void> updateByKey(@RequestBody Article article){
        log.info("article:{}",article);
        articleService.updateByKey(article);
        return JsonResult.ok("修改成功");
    }

    @PostMapping("/update-operate-by-key")
    @ApiOperation("通过主键修改文章内容")
    public JsonResult<Void> updateOperateByKey(@RequestBody ArticleOperateDTO article){
        log.info("article:{}",article);
        articleService.updateOperateByKey(article);
        return JsonResult.ok("修改成功");
    }


    /****************        查        ******************************************************************/
    /**
     * 根据查询条件查找列表
     * @param articleSimpleDTO 条件
     * @return JsonResult<JsonPage<ArticleVO>>
     */
    @GetMapping("/get-list-by-nation-id")
    @ApiOperation("通过一定条件查找列表")
    public JsonResult<List<Article>> getListByExample(ArticleSimpleDTO articleSimpleDTO){
        log.info("articleDTO:{}", articleSimpleDTO);
        List<Article> listByNationId = articleService.getListByNationId(articleSimpleDTO.getNationId());
        return JsonResult.ok("查询成功",listByNationId);
    }

    @GetMapping("/get-article-detail")
    @ApiOperation("查询文章详情")
    public JsonResult<ArticleDetailVO> getArticleDetail(ArticleSimpleDTO articleSimpleDTO){
        log.info("articleDTO:{}", articleSimpleDTO);
        ArticleDetailVO articleDetail = articleService.getArticleDetail(articleSimpleDTO.getId());

        return JsonResult.ok("查询成功",articleDetail);
    }

    @GetMapping("/get-by-nation")
    @ApiOperation("查询文章通过民族")
    public JsonResult<List<Article>> selectByNation(Long id){
        log.info("id:{}", id);
        List<Article> articles = articleService.selectByNation(id);

        return JsonResult.ok("查询成功",articles);
    }

    @GetMapping("/get-all-list")
    @ApiOperation("查询所有文章")
    public JsonResult<List<Article>> getAllList(Long categoryId){
        List<Article> allList = articleService.getAllList(categoryId);

        return JsonResult.ok("查询成功",allList);
    }

    @GetMapping("/get-list-by-user")
    @ApiOperation("查询所有文章")
    public JsonResult<JsonPage<Article>> getAllListByUser(Long userId, Integer pageNum, Integer pageSize){
        JsonPage<Article> jsonPage = articleService.getAllListByUser(userId,pageNum,pageSize);

        return JsonResult.ok("查询成功",jsonPage);
    }
    @GetMapping("/get-list-by-user-and-collect")
    @ApiOperation("查询所有文章")
    public JsonResult<JsonPage<Article>> getAllListByUserAndCollect(Long userId, Integer pageNum, Integer pageSize){
        JsonPage<Article> jsonPage = articleService.getAllListByUserAndCollect(userId,pageNum,pageSize);

        return JsonResult.ok("查询成功",jsonPage);
    }


    @GetMapping("/get-all-list-management")
    @ApiOperation("查询所有文章")
    public JsonResult<JsonPage<ArticleDetailVO>> getAllListManagement(ArticleSimplePageDTO articleSimplePageDTO){
        JsonPage<ArticleDetailVO> allListManagement = articleService.getAllListManagement(articleSimplePageDTO);

        return JsonResult.ok("查询成功",allListManagement);
    }









}
